<?php

namespace app\purchase\controller;

use think\Request;
use think\Exception;
use app\purchase\model\Client as ClientModel;
use app\purchase\model\Clientbill as ClientbillModel;
class Client extends \PurchaseController
{

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $ClientModel = new ClientModel();
        try {

            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $ClientModel = new ClientModel();

        try {
            $data = $ClientModel->alias('c')->relation('level,department,salesman,paymentType,clientbill')->where('c.client_id',$id)->find()->toArray();


           $data = array_merge($data,$data['level'],$data['department'],$data['salesman'],$data['paymentType'],$data['clientbill']);

            if(!empty($data['sign_start']))$data['sign_start'] = date('Y-m-d',$data['sign_start']);
            if(!empty($data['sign_end'])) $data['sign_end'] = date('Y-m-d',$data['sign_end']);
            unset($data['is_delete']);
            unset($data['password']);
            unset($data['department']);
            unset($data['level']);
            unset($data['salesman']);
            unset($data['clientbill']);
            $data['area'] = explode(',',$data['area']);
            unset($data['paymentType']);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function getName($id)
    {
        $ClientModel = new ClientModel();
        $name = $ClientModel->where('client_id',$id)->value('client_name');
        return $name;
    }
    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function update(Request $request)
    {

        $ClientModel = new ClientModel();
        $ClientbillModel = new ClientbillModel();
        $Area = new \app\shop\model\Area();
        $params = $request->post();

            try {
            if(!empty($params['date'][0])&&!empty($params['date'][1])){
                $params['sign_start'] = strtotime($params['date'][0]);
                $params['sign_end'] = strtotime($params['date'][1]);
            }
                $params['area'] = $Area->tSrt($params['area']);
                $ClientModel->startTrans();
                $customer_data = array(
                    'client_name' => $params['client_name'],
                    'level_id' => $params['level_id'],
                    'sign_start' => $params['sign_start'],
                    'sign_end' => $params['sign_end'],
                    'warehouse_id' => $params['warehouse_id'],
                    'department_id' => $params['department_id'],
                    'salesman_id' => $params['salesman_id'],
                    'client_sn' => $params['client_sn'],
                    'zipcode' => $params['zipcode'],
                    'fax' => $params['fax'],
                    'area' => $params['area'],
                    'address' => $params['address'],
                    'logistics_code' => $params['logistics_code'],
                    'extra' => $params['extra'],
                    'name' => $params['name'],
                    'mobile' => $params['mobile'],
                    'phone' => $params['phone'],
                    'position' => $params['position'],
                    'email' => $params['email'],
                    'qq' => $params['qq'],
                    'is_dh' => $params['is_dh'],

                );
                $ClientModel->where('client_id',$params['client_id'])->update($customer_data);

                $Clientbill = array(
                'invoice' => $params['invoice'],
                'tax_num' => $params['tax_num'],
                'invoice_address' => $params['invoice_address'],
                'invoice_mobile' => $params['invoice_mobile'],
                'bank_name' => $params['bank_name'],
                'bank' => $params['bank'],
                'bank_account' => $params['bank_account'],
                'pdt_id' => $params['pdt_id']
            );
                $ClientbillModel->where('client_id',$params['client_id'])->update( $Clientbill);







                $ClientModel->commit();
            $this->successTerminate($ClientModel);
        } catch (Exception $e) {
                $ClientModel->rollback();
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $ClientModel = new ClientModel();
        try {
            $ClientModel->where('client_id',$id)->update(['is_delete'=>1]);
            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 重置客户密码
     */
    public function resetPass(Request $request){

        $ClientModel = new ClientModel();

        $params = $request->post();
        try {
            $params['salt'] = $ClientModel->generateSalt();
            $params['password'] = md5($params['password'] . $params['salt']);
            $ClientModel->where('client_id',$params['client_id'])->update($params);

            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }

    }

    /**
     * 批量修改状态
     */
    public function batchDeal(Request $request){

        $params = $request->post();

        $status = $params['status'];

        try {

            foreach ($params['client_ids'] as $id) {
                $ClientModel = new ClientModel();
                $flag = $ClientModel->where('client_id',$id)->value('is_dh');
                if(!empty($flag)){
                    $ClientModel->where('client_id',$id)->update(['is_dh'=>$status]);
                }
            }

            $this->successTerminate();

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 获取简单数据
     */
    public function getSimpleClient(){
        try {
            $ClientModel = new ClientModel();

            $data = $ClientModel->field('client_id,client_name,level_id')->select();

            $this->successTerminate($data);

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //通过客户ID查询发票信息
    public function getBill(Request $request){
        try {
            $client_id = \request()->header('company');
            $ClientbillModel = new ClientbillModel();
            $data = $ClientbillModel->where('client_id',$client_id)->find();
            $this->successTerminate($data);

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }


    }
    //D修改发票信息
    public function updateBill(Request $request){
        try {
            $params = $request->post();
            $params['client_id'] = \request()->header('company');
            $ClientbillModel = new ClientbillModel();
            $ClientbillModel->where('client_id',$params['client_id'])->update($params);
            $data = $ClientbillModel->where('client_id',$params['client_id'])->find();
            $this->successTerminate($data);

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //获取公司信息
    public function getCompanyInfo(Request $request){
        try {
            $params = $request->post();
            $params['client_id'] = \request()->header('company');
            $ClientModel = new ClientModel();
            $client = $ClientModel->where('client_id',$params['client_id'])->field('client_id,client_name,area,fax,address,phone')->find();
            $client['area'] = !empty($client['area']) ? explode(',',$client['area']) : [];
            $ClientbillModel = new ClientbillModel();
            $bill = $ClientbillModel->where('client_id',$params['client_id'])->field('id,invoice,tax_num,invoice_address,invoice_mobile,bank_name,bank,bank_account')->find();
            $data = [
                'client' => $client,
                'bill' => $bill
            ];
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //更新公司信息
    public function updateCompanyInfo(Request $request){
        try {
            $params = $request->post();

            $client_id = \request()->header('company');

            $ClientModel = new ClientModel();
            $ClientbillModel = new ClientbillModel();
            $ClientModel->where('client_id',$client_id)->update($params['client']);
            $ClientbillModel->where('client_id',$client_id)->update($params['bill']);

            $this->successTerminate();

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
}